<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Kafka事务机制深度解析</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .content-card {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            transition: all 0.3s ease;
        }
        
        .content-card:hover {
            transform: translateY(-2px);
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
        }
        
        .section-title {
            position: relative;
            padding-left: 20px;
        }
        
        .section-title::before {
            content: '';
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
            width: 4px;
            height: 24px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 2px;
        }
        
        .highlight-box {
            background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
            color: white;
            padding: 20px;
            border-radius: 12px;
            margin: 20px 0;
        }
        
        .feature-icon {
            width: 60px;
            height: 60px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
            font-size: 24px;
            margin-bottom: 16px;
        }
        
        .mermaid {
            background: white;
            padding: 20px;
            border-radius: 12px;
            box-shadow: 0 4px 20px rgba(0, 0, 0, 0.05);
        }
        
        .drop-cap {
            float: left;
            font-size: 4em;
            line-height: 0.8;
            margin: 0.1em 0.1em 0 0;
            font-weight: 700;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            font-family: 'Noto Serif SC', serif;
        }
        
        .concept-card {
            background: white;
            border-radius: 16px;
            padding: 24px;
            box-shadow: 0 4px 20px rgba(0, 0, 0, 0.05);
            transition: all 0.3s ease;
            border: 1px solid #e5e7eb;
        }
        
        .concept-card:hover {
            transform: translateY(-4px);
            box-shadow: 0 12px 40px rgba(0, 0, 0, 0.1);
            border-color: #667eea;
        }
        
        .timeline-item {
            position: relative;
            padding-left: 40px;
            margin-bottom: 30px;
        }
        
        .timeline-item::before {
            content: '';
            position: absolute;
            left: 10px;
            top: 8px;
            width: 12px;
            height: 12px;
            background: #667eea;
            border-radius: 50%;
            box-shadow: 0 0 0 4px rgba(102, 126, 234, 0.2);
        }
        
        .timeline-item::after {
            content: '';
            position: absolute;
            left: 15px;
            top: 20px;
            width: 2px;
            height: calc(100% + 10px);
            background: #e5e7eb;
        }
        
        .timeline-item:last-child::after {
            display: none;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <div class="hero-gradient text-white py-20">
        <div class="container mx-auto px-6">
            <div class="max-w-4xl mx-auto text-center">
                <h1 class="text-5xl md:text-6xl font-bold mb-6 leading-tight">
                    Kafka事务机制
                    <span class="block text-3xl md:text-4xl mt-4 font-light opacity-90">深度技术解析</span>
                </h1>
                <p class="text-xl md:text-2xl opacity-90 leading-relaxed">
                    探索分布式系统中的数据一致性保障
                </p>
                <div class="mt-10 flex justify-center space-x-6">
                    <div class="text-center">
                        <i class="fas fa-shield-alt text-4xl mb-2"></i>
                        <p class="text-sm">原子性保证</p>
                    </div>
                    <div class="text-center">
                        <i class="fas fa-sync-alt text-4xl mb-2"></i>
                        <p class="text-sm">精确一次处理</p>
                    </div>
                    <div class="text-center">
                        <i class="fas fa-database text-4xl mb-2"></i>
                        <p class="text-sm">数据一致性</p>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div class="container mx-auto px-6 py-12">
        <!-- Introduction -->
        <div class="content-card rounded-2xl shadow-xl p-8 mb-8">
            <p class="text-lg leading-relaxed text-gray-700">
                <span class="drop-cap">K</span>afka作为一个高性能的流处理平台，不仅支持海量数据的实时传输，还提供了复杂的事务机制来确保数据的一致性和完整性。在现代分布式系统中，当你的应用需要在多个服务之间传递数据时——比如电商平台在订单处理的各个环节中——必须确保数据的完整性。Kafka的事务机制允许我们将一系列的消息发送操作组合成一个原子操作，要么全部成功，要么全部失败，从而保证数据的完整性。
            </p>
        </div>

        <!-- Transaction Architecture Visualization -->
        <div class="mb-12">
            <h2 class="section-title text-3xl font-bold text-gray-800 mb-6">事务架构概览</h2>
            <div class="mermaid">
                graph TB
                    subgraph "Kafka事务架构"
                        A[生产者应用] -->|1.开启事务| B[事务协调器]
                        B -->|2.分配事务ID| C[事务日志]
                        A -->|3.发送消息| D[Kafka Broker]
                        D -->|4.写入日志| E[主题分区]
                        A -->|5.提交/回滚| B
                        B -->|6.更新状态| C
                        C -->|7.通知| F[消费者]
                        
                        style A fill:#667eea,stroke:#fff,stroke-width:2px,color:#fff
                        style B fill:#764ba2,stroke:#fff,stroke-width:2px,color:#fff
                        style F fill:#f093fb,stroke:#fff,stroke-width:2px,color:#fff
                    end
            </div>
        </div>

        <!-- Basic Concepts -->
        <div class="mb-12">
            <h2 class="section-title text-3xl font-bold text-gray-800 mb-6">
                <i class="fas fa-book-open mr-3 text-purple-600"></i>事务基本概念
            </h2>
            
            <div class="grid md:grid-cols-2 gap-6">
                <div class="concept-card">
                    <div class="feature-icon">
                        <i class="fas fa-atom"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-3 text-gray-800">ACID特性</h3>
                    <ul class="space-y-2 text-gray-600">
                        <li><i class="fas fa-check-circle text-green-500 mr-2"></i><strong>原子性：</strong>事务中的所有操作要么全部成功，要么全部失败</li>
                        <li><i class="fas fa-check-circle text-green-500 mr-2"></i><strong>一致性：</strong>事务执行前后，系统状态保持一致</li>
                        <li><i class="fas fa-check-circle text-green-500 mr-2"></i><strong>隔离性：</strong>并发事务之间互不干扰</li>
                        <li><i class="fas fa-check-circle text-green-500 mr-2"></i><strong>持久性：</strong>事务提交后，修改永久保存</li>
                    </ul>
                </div>
                
                <div class="concept-card">
                    <div class="feature-icon">
                        <i class="fas fa-bullseye"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-3 text-gray-800">事务目标</h3>
                    <ul class="space-y-2 text-gray-600">
                        <li><i class="fas fa-shield-alt text-blue-500 mr-2"></i><strong>消息原子性：</strong>确保一组相关消息的发送要么全部成功，要么全部失败</li>
                        <li><i class="fas fa-sync text-purple-500 mr-2"></i><strong>精确一次处理：</strong>支持exactly-once语义，避免消息重复或丢失</li>
                        <li><i class="fas fa-network-wired text-indigo-500 mr-2"></i><strong>跨主题事务：</strong>允许在多个主题之间进行原子性操作</li>
                    </ul>
                </div>
            </div>
        </div>

        <!-- Transaction Components -->
        <div class="mb-12">
            <h2 class="section-title text-3xl font-bold text-gray-800 mb-6">
                <i class="fas fa-cogs mr-3 text-purple-600"></i>事务机制组成
            </h2>
            
            <div class="grid md:grid-cols-3 gap-6">
                <div class="content-card p-6 rounded-xl">
                    <i class="fas fa-user-cog text-4xl text-purple-600 mb-4"></i>
                    <h3 class="text-xl font-bold mb-3">生产者事务性支持</h3>
                    <p class="text-gray-600 mb-3">配置事务性生产者，启用幂等性保证</p>
                    <code class="bg-gray-100 px-3 py-1 rounded text-sm">enable.idempotence=true</code>
                </div>
                
                <div class="content-card p-6 rounded-xl">
                    <i class="fas fa-tasks text-4xl text-indigo-600 mb-4"></i>
                    <h3 class="text-xl font-bold mb-3">事务状态管理</h3>
                    <div class="space-y-2 text-sm">
                        <div class="flex items-center">
                            <span class="w-20 font-semibold">Pending</span>
                            <span class="text-gray-600">事务进行中</span>
                        </div>
                        <div class="flex items-center">
                            <span class="w-20 font-semibold">Committed</span>
                            <span class="text-gray-600">事务已提交</span>
                        </div>
                        <div class="flex items-center">
                            <span class="w-20 font-semibold">Aborted</span>
                            <span class="text-gray-600">事务已回滚</span>
                        </div>
                    </div>
                </div>
                
                <div class="content-card p-6 rounded-xl">
                    <i class="fas fa-file-alt text-4xl text-pink-600 mb-4"></i>
                    <h3 class="text-xl font-bold mb-3">事务日志</h3>
                    <p class="text-gray-600">记录事务操作和状态变化，支持故障恢复和数据一致性保证</p>
                </div>
            </div>
        </div>

        <!-- Transaction Flow -->
        <div class="mb-12">
            <h2 class="section-title text-3xl font-bold text-gray-800 mb-6">
                <i class="fas fa-stream mr-3 text-purple-600"></i>事务实现流程
            </h2>
            
            <div class="content-card rounded-xl p-8">
                <div class="space-y-6">
                    <div class="timeline-item">
                        <h4 class="font-bold text-lg mb-2">1. 初始化事务</h4>
                        <p class="text-gray-600">创建事务性生产者，配置transactional.id，启用幂等性支持</p>
                    </div>
                    
                    <div class="timeline-item">
                        <h4 class="font-bold text-lg mb-2">2. 开始事务</h4>
                        <p class="text-gray-600">调用beginTransaction()方法，分配唯一事务ID，状态设为Pending</p